package b2.widget.tab
{
	import b2.net.*;
	import b2.util.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.ui.Mouse;

	public class TabPageSelector extends Sprite
	{
		private var firstPage:Sprite;
		private var lastPage:Sprite;
		private var pageUp:Sprite;
		private var pageDown:Sprite;
		public var pageNumArea:TabPageNumArea;
		public var maxPageNum:int;
		private const gap:int = 5;
		
		private var tab:Tab;
		
		public function TabPageSelector(tab:Tab)
		{
			super();
			this.tab = tab;
			//首页
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage");
			firstPage = new TabPageFirstPage();
			addChild(firstPage);
			firstPage.addEventListener(MouseEvent.CLICK, onFirstPageClick);
			firstPage.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			firstPage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			var bottomNavigatorPosYGap:int = 20;
			//上一页
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			pageUp = new TabPagePageUp();
			pageUp.x = firstPage.width + gap;
			addChild(pageUp);
			pageUp.addEventListener(MouseEvent.CLICK, onPageUpClick);
			pageUp.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			pageUp.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			pageNumArea = new TabPageNumArea(tab);
			pageNumArea.x = pageUp.x + pageUp.width + gap;
			addChild(pageNumArea);
			//下一页
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			pageDown = new TabPagePageDown();
			pageDown.x = pageNumArea.x + pageNumArea.width + gap;
			addChild(pageDown);
			pageDown.addEventListener(MouseEvent.CLICK, onPageDownClick);
			pageDown.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			pageDown.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			//末页
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage");
			lastPage = new TabPageLastPage();
			lastPage.x = pageDown.x + pageDown.width + gap;
			addChild(lastPage);
			lastPage.addEventListener(MouseEvent.CLICK, onLastPageClick);
			lastPage.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			lastPage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function refreshTotalPageNum():void
		{
			removeChild(pageNumArea);
			pageNumArea = new TabPageNumArea(tab, maxPageNum);
			firstPage.x = 52 - pageNumArea.width / 2 - firstPage.width - pageUp.width - 2 * gap;
			pageUp.x = 52 - pageNumArea.width / 2 - pageUp.width - gap;
			pageNumArea.x = 52 - pageNumArea.width / 2;
			pageDown.x = 52 + pageNumArea.width / 2 + gap;
			lastPage.x = 52 + pageNumArea.width / 2 + pageDown.width + 2 * gap;
			addChild(pageNumArea);
		}
		
		private function onFirstPageClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex > 0)
			{
				tab.currentPageIndex = 0;
			}
		}
		
		private function onPageUpClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex > 0)
			{
				tab.currentPageIndex--;
			}
		}
		
		private function onPageDownClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex < tab.totalPageNum - 1)
			{
				tab.currentPageIndex++;
			}
		}
		
		private function onLastPageClick(event:MouseEvent):void
		{
			if (tab.currentPageIndex < tab.totalPageNum - 1)
			{
				tab.currentPageIndex = tab.totalPageNum - 1;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			event.target.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			event.target.filters = null;
		}
	}
}